<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .box {
      position: absolute;
      width: 100px;
      height: 100px;
      background: yellow;
    }
  </style>
</head>

<body>
  <div class="box"></div>
  <script>
    // + 让光标在box的中间位置
    // + 盒子边界值的判断（box只能在可是窗口移动）

    let box = document.querySelector(".box");

    //1：鼠标按下的时候，证明box盒子在在window中做鼠标移动事件
    box.onmousedown = function () {
      window.onmousemove = function (e) {
        //鼠标按下的时候，获取到box元素的初始值，赋值给box的left和top
        var e = e || window.event;
        var top = e.pageY - (box.offsetWidth / 2);
        var left = e.pageX - (box.offsetHeight / 2);
        // console.log(top, left);
        moveF(top, left)

        function moveF(top, left) {
          box.style.top = top + "px"
          box.style.left = left + "px"
        }

        //需求3：设置盒子的边界，也就是说鼠标向左右上下边界移动不能小于0px
        top = top <= 0 ? top = 0 : top
        left = left <= 0 ? left = 0 : left
        moveF(top, left)

        if (parseInt(box.style.top) >= innerHeight - box.offsetHeight) {
          box.style.top = innerHeight - box.offsetHeight + "px"
        }
        if (parseInt(box.style.left) >= innerWidth - box.offsetWidth) {
          box.style.left = innerWidth - box.offsetWidth + "px"
        }
      }

    }
    //2：鼠标抬起的时候，把鼠标按下事件内的window鼠标移动事件-取消为空
    box.onmouseup = function () {
      box.onmousedown = function () {
        window.onmousemove = null
      }
    }
  </script>
</body>
<!-- let top = e.pageY - (box.offsetWidth / 2);
let left = e.pageX - (box.offsetHeight / 2);
// console.log(top, left);
box.style.top = top + "px"
box.style.left = left + "px" -->

<!-- //(3)我们mask的移动距离
var maskX = x - mask.offsetWidth / 2;
var maskY = y - mask.offsetHeight / 2; -->
<!-- //判断出mask盒子的位置

maskX = maskX <= 0 ? maskX = 0:maskX;
maskX = maskX >= maskMax ? maskX = maskMax : maskX;
maskY = maskY <= 0 ? maskY = 0:maskY;
maskY = maskY >= maskMax ? maskY = maskMax : maskY; -->

</html>